package org.ejc.api.email;

import java.util.List;

import javax.ejb.SessionBean;
import javax.jms.JMSException;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
import org.ejc.email.EmailMessage;
import org.ejc.persist.model.ConventionRegistrant;
import org.ejc.persist.model.Registrant;
import org.ejc.util.EJCUtils;
import org.ejc.util.JMSUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

/**
 * @author zac
 * 
 * @ejb.bean type="Stateless" name="EmailAPI" description="EmailAPI session
 *           bean" local-jndi-name="org.ejc/EmailAPILocalHome"
 *           jndi-name="org.ejc/EmailAPIHome" view-type="both"
 * 
 * @ejb.ejb-ref ejb-name="EmailAPI" ref-name="ejb/EmailAPI" view-type="both"
 * 
 * @ejb.transaction type="RequiresNew"
 * @ejb.util generate = "physical"
 */
public abstract class EmailAPIBean implements SessionBean {

	private static Logger log = Logger.getLogger(EmailAPIBean.class);

	String EOL = System.getProperty("line.separator");

	/**
	 * send email
	 * 
	 * @ejb.interface-method view-type="both"
	 * 
	 */
	public void sendEmailToRegistrant(Integer registrantId) {
		Session hsession = null;
		Transaction tx = null;
		try {
			hsession = EJCUtils.getHibernateSession();
			Registrant registrant = (Registrant) hsession.load(
					Registrant.class, registrantId);

			ConventionRegistrant conventionRegistrant = null;
			Criteria crit = hsession.createCriteria(ConventionRegistrant.class);
			crit.add(Expression.eq("registrantId", registrant.getRegistrantId()));

			List list = crit.list();
			if (list != null && list.size() > 0) {
				conventionRegistrant = (ConventionRegistrant) list.get(0);
			}

			String emailAddress = registrant.getEmail();
			String firstName = registrant.getFirstName();
			String lastName = registrant.getLastName();

			EmailMessage emailMessage = new EmailMessage();
			// ok got the email.
			emailMessage.setRecipent(emailAddress);
			emailMessage.setSubject("Welcome to EJC 2006");
			emailMessage.setIncludeFooter(true);

			StringBuffer message = new StringBuffer();
			message.append("Dear " + firstName + " " + lastName + EOL + EOL
					+ "Welcome to EJC 2006. Thanks for registering with us! "+EOL);

			if (conventionRegistrant.getPreReg() != null) {
				message.append("Your Pre-Regisration code is "
						+ conventionRegistrant.getPreReg());
				message
						.append("\nPlease keep this safe as it will be needed to get access to the convention");
			}
			
			message.append(EOL);
			message.append("See you in Cork 2006."+EOL);
			message.append("\nBest Regards \n");
			message.append("-EJC 2006 Team!");

			emailMessage.setMessageText(message.toString());
			try {
				JMSUtil.sendToEmailQ("queue/mail", emailMessage);
			} catch (NamingException e) {
				log.fatal("Naming Exception while trying to send email !", e);
			} catch (JMSException e) {
				log.fatal("JMS Exception while trying to send email !", e);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			hsession.close();
		}

	}

}
